import { ref, computed } from 'vue'
import { useIntervalFn } from '@vueuse/core'
import dayjs from 'dayjs'

function useCountDown () {
  // 计时响应数据
  const time = ref(0)
  const formatTime = computed(() => {
    return dayjs.unix(time.value).format('mm分ss秒')
  })
  const { pause, resume } = useIntervalFn(() => {
    if (time.value === 0) {
      return pause()
    }
    time.value--
  })
  // 开始函数
  function start (startTime) {
    // 倒计时初始值
    time.value = startTime
    // 倒计时
    resume()
  }
  // 结束函数
  function stop (time) {
    pause()
  }
  return {
    time,
    formatTime,
    start,
    stop
  }
}

export default useCountDown
